<?php
/**
 * Created by PhpStorm
 * user: MingLiang
 * Date：2021/8/20
 * Time: 3:56 下午
 */


namespace app\admin\controller\cost;


use app\admin\model\CostClassify;
use app\admin\model\CostCompany;
use app\admin\model\CostPurchaselist;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\tool\CommonTool;
use EasyAdmin\upload\Uploadfile;
use jianyan\excel\Excel;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use think\App;
use EasyAdmin\annotation\NodeAnotation;
use think\facade\Db;

/**
 * Class Company
 * @package app\admin\controller\cost
 * @ControllerAnnotation(title="公司管理")
 */

class Company extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new CostCompany();
    }

    /**
     * @NodeAnotation(title="上传")
     */
    public function  uploadFile(){
        $data = [
            'upload_type' => 'local',
            'file'        => $this->request->file('file'),
        ];
        $uploadConfig = sysconfig('upload');
        empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
        try {
            $upload = Uploadfile::instance()
                ->setUploadType($data['upload_type'])
                ->setUploadConfig($uploadConfig)
                ->setFile($data['file'])
                ->save();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }


        if ($upload['save'] == true) {
            $arr = Excel::import($upload['name']);
            unset($arr[1]);
            foreach ($arr as $key => $value){
                $insert[] = [
                   'company_name' => $value[1] ?? '',
                   'remark' => $value[2] ?? '',
                ];
            }
            $type = CostCompany::create_data($insert);
            if($type){
                return json(['code'=> 200,'msg' => '上传成功','data' => '']);
            }else{
                return json(['code'=> 500,'msg' => '上传失败','data' => '']);
            }

        } else {
            return json(['code'=> 500,'msg' => $upload['msg'],'data' => '']);
        }
    }


    /**
     * @NodeAnotation(title="导出")
     */
    public function export()
    {
        list($page, $limit, $where) = $this->buildTableParames();
        $header = [
            ['序号','id'],
            ['公司名称','company_name'],
            ['备注说明','remark'],
            ['创建时间','create_time'],
        ];
        $list = $this->model
            ->where($where)
            ->limit(100000)
            ->order('id', 'desc')
            ->select()
            ->toArray();
        array_walk($list,function(&$v,$i){
            $v['id'] = $i+1;
            unset($v['status']);
            unset($v['sort']);
        });
        $fileName = time();
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }

    /**
     * @NodeAnotation(title="下载模板")
     */
    public function downloadTpl(){
        $header = [
            ['序号','id'],
            ['公司名称','company_name'],
            ['备注说明','remark'],
        ];
        $list = [
            [
                'id'=>1,
                'company_name' => '北京总部',
                'remark' => '北京总部',
            ]
        ];
        return Excel::exportData($list, $header, '公司上传模板', 'xlsx');
    }

}